package com.wsj.a.collapsingdemo;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.baidu.mobstat.StatService;
import com.wsj.a.collapsingdemo.databinding.ActivityScrollingBinding;

import java.util.ArrayList;
import java.util.List;

public class ScrollingActivity extends AppCompatActivity {

    private ActivityScrollingBinding mBinding;
    private String nickname;
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(ScrollingActivity.this,R.layout.activity_scrolling);
        mBinding.setOnClick(this);
        mBinding.title.setText("小印象用户");
        initEvent();
        setUserInfo();
        initViewPager();

        // 建议在`自定义页面`的页面开始函数中调用
        StatService.onPageStart(ScrollingActivity.this,"用户中心页面");

    }


    public void initEvent(){
        mBinding.appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if(verticalOffset == 0){
                    //展开状态
                    mBinding.toolbarBg.setAlpha(0);
                    mBinding.rlToolbar.setAlpha(0);
                    mBinding.toolbarMore.setVisibility(View.GONE);
                    mBinding.toolbarTitle.setVisibility(View.GONE);
                    mBinding.toolbarBack.setVisibility(View.GONE);


                }else if(Math.abs(verticalOffset) < appBarLayout.getTotalScrollRange()) {
                    int value = Math.abs(verticalOffset);
                    float alpha = (float) value / appBarLayout.getTotalScrollRange()*3;
                    if(alpha<1){
                        mBinding.toolbarBg.setAlpha(alpha);
                    }else{
                        mBinding.toolbarBg.setAlpha(1);
                    }
                    mBinding.rlToolbar.setAlpha(0);
                    mBinding.toolbarMore.setVisibility(View.GONE);
                    mBinding.toolbarTitle.setVisibility(View.GONE);
                    mBinding.toolbarBack.setVisibility(View.GONE);

                }else if(Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()){
                    mBinding.toolbarBg.setAlpha(1);

                    mBinding.rlToolbar.setAlpha(1);
                    mBinding.toolbar.setVisibility(View.VISIBLE);
                    mBinding.toolbarMore.setVisibility(View.VISIBLE);
                    mBinding.toolbarTitle.setText("小印象用户");
                    mBinding.toolbarTitle.setVisibility(View.VISIBLE);
                    mBinding.toolbarBack.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    //设置个人信息
    public void setUserInfo(){
        mBinding.focus.setText(StringUtils.getColorKeyString("关注 12","12",30, getResources().getColor(R.color.main_color)));
        mBinding.fans.setText(StringUtils.getColorKeyString("粉丝 12","12",30, getResources().getColor(R.color.main_color)));
    }

    //设置可切换页面信息
    public void initViewPager(){
        List<Fragment> mFragments = new ArrayList<>();
        mFragments.add(new VideoFragement());
        mFragments.add(new VideoFragement());
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mAdapter.setData(mFragments);
        mBinding.includeLayout.vp.setAdapter(mAdapter);
        mBinding.includeLayout.vp.setCurrentItem(0);
        mBinding.tabLayout.setupWithViewPager(mBinding.includeLayout.vp);
        mBinding.includeLayout.vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
               mBinding.includeLayout.nestedScrollView.scrollTo(0, 0);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void back(View view){
        this.finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 建议在`自定义页面`的页面结束函数中调用
        StatService.onPageEnd(ScrollingActivity.this, "用户中心页面");
    }


}
